﻿function Ajax() {
    this.ajax = function (url, requestType, data, callback) {
        $.ajax({
            crossDomain: true,
            url: url,
            headers: {
                'Token': layui.sessionData('user').token
            },
            type: requestType,
            contentType: "application/json;charset=utf-8",
            data: data,
            success: callback,
            beforeSend: function () {
                layer.load(2);
            },
            error: function (res) {
                if (res.status == 0) {
                    layer.msg("网络连接超时");
                }
                else if (res.status == 404) {
                    layer.msg("网络连接失败");
                }
                else {
                    layer.msg("网络连接错误");
                }
            },
            complete: function () {
                layer.closeAll('loading');
            }
        });
    }
}
Ajax.prototype.get = function (url, data, callback) {
    this.ajax(url, 'GET', JSON.stringify(data), callback)
}

Ajax.prototype.post = function (url, data, callback) {
    this.ajax(url, 'POST', JSON.stringify(data), callback)
}

Ajax.prototype.put = function (url, data, callback) {
    this.ajax(url, 'PUT', data, callback)
}

Ajax.prototype.delete = function (url, data, callback) {
    this.ajax(url, 'DELETE', data, callback)
}

Ajax.prototype.constructor = Ajax